//
//  DailyViewController.m
//  OpenEyes
//
//  Created by apple on 15/8/10.
//  Copyright (c) 2015年 袁阿迅. All rights reserved.
//

#import "DailyCollectionViewCell.h"
#import "DailyViewController.h"
#import "PKRequestManager.h"
#import "DailyModel.h"
#import "Header.h"
#import "DailyFooterReusableView.h"
#import "TotalScrollViewController.h"
#import "MenuView.h"
#import "DailyCollectionViewController.h"
#import "KindsCollectionViewController.h"
#import "KindsInfoController.h"
#import "MyCollectionViewController.h"
#import "FunctionTableViewController.h"
#import "MyCacheViewController.h"
#import "MoreViewController.h"



@interface DailyViewController ()

/** 导航栏左边按钮弹出来的视图*/
@property (nonatomic, strong) MenuView * menuView;

@property (nonatomic ,strong) DailyCollectionViewController *collection;

@property (nonatomic ,strong) KindsCollectionViewController *kindController;

@property (nonatomic ,assign) int tag;

@end

@implementation DailyViewController

/**
 *  viewDidLoad
 */
- (void)viewDidLoad
{
    [super viewDidLoad];

    
    //添加导航栏左边按钮
    [self addNavigationView];
    [self addSegmented];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0];
}
/**
 *  添加导航栏标题
 */
- (void)addSegmented
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self action:@selector(selectedAction:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"页面切换" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self selectedAction:button];
    [self createCollectionView];
    [self creatCollectionView];
    self.navigationItem.titleView = button;
}
/**
 *  导航栏标题切换
 *
 *  @param Seg 接收选择的下标
 */
- (void)selectedAction:(UIButton *)Seg
{
    if (self.tag == 100)
    {
        [_kindController.view hinge:^{
            self.tag = 200;
            [self.view bringSubviewToFront:_collection.view];
        }];
        
    }
    else
    {
        [_collection.view hinge:^{
            self.tag = 100;
            [self.view bringSubviewToFront:_kindController.view];
        }];
        
    }
}

/**
 *  菜单视图
 *
 *  @return 菜单视图
 */
- (MenuView *)menuView
{
    if (!_menuView)
    {
        self.menuView = [[MenuView alloc] initWithFrame:CGRectMake(0, -SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _menuView.backgroundColor = [UIColor whiteColor];
        [_menuView.saveBtn addTarget:self action:@selector(pushNextView) forControlEvents:UIControlEventTouchUpInside];
        [_menuView.downloadBtn addTarget:self action:@selector(pushDownloadViewController) forControlEvents:UIControlEventTouchUpInside];
        [_menuView.functionOnOff addTarget:self action:@selector(pushFunctionOnOff) forControlEvents:UIControlEventTouchUpInside];
    }
    return _menuView;
}
/**
 *  我的收藏
 */
- (void)pushNextView
{
    MyCollectionViewController *collectionVC = [[MyCollectionViewController alloc] init];
    [self.navigationController pushViewController:collectionVC animated:YES];
}
/**
 *  我的缓存
 */
- (void)pushDownloadViewController
{
    MyCacheViewController *cacheVC = [[MyCacheViewController alloc] init];
    [self.navigationController pushViewController:cacheVC animated:YES];
}
/**
 *  功能开关
 */
- (void)pushFunctionOnOff
{
    FunctionTableViewController *function = [[FunctionTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
    [self.navigationController pushViewController:function animated:YES];
}

/**
 *  添加左导航栏按钮
 */
- (void)addNavigationView
{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 44, 44);
    [leftButton setBackgroundImage:[UIImage imageWithName:@"ic_action_menu"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationController.navigationBar.translucent = NO;    
}

/**
 *  导航栏左边按钮触发方法
 *
 *  @param changePage changePage
 */
- (void)changePage:(UIButton *)changePage
{
    //添加右边导航按钮
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 0, 44, 44);
    [rightButton setBackgroundImage:[UIImage imageWithName:@"ic_action_settings"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(changeRightNavi:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
    [self.view addSubview:self.menuView];
    
    //如果弹出视图是负的，就是隐藏的时候
    if (_menuView.frame.origin.y == -SCREEN_HEIGHT)
    {
        [UIView animateWithDuration:0.5 animations:^{
            //先获取弹出视图的大小
            CGRect rect = _menuView.frame;
            //改变弹出视图的大小的距离y的值.(弹出)
            rect.origin.y = 0;
            //重新给这个视图新的位置
            _menuView.frame = rect;
            self.navigationItem.titleView.hidden = YES;
            //按钮的位置旋转一180度 CGAffineTransformMakeRotation（旋转）
            changePage.transform = CGAffineTransformRotate(changePage.transform, M_PI_2);
        }];
    }
    else
    {
        [UIView animateWithDuration:0.5 animations:^{
            CGRect rect = _menuView.frame;
            rect.origin.y = -SCREEN_HEIGHT;
            _menuView.frame = rect;
            changePage.transform = CGAffineTransformRotate(changePage.transform, -M_PI_2);
            self.navigationItem.rightBarButtonItem = nil;
            self.navigationItem.titleView.hidden = NO;
        } completion:^(BOOL finished) {
        }];
    }
}
/**
 *  弹出左边的视图的右边导航栏按钮的点击事件
 *
 *  @param sendValue sendValue
 */
- (void)changeRightNavi:(UIButton *)sendValue
{
    MoreViewController *moreVC = [[MoreViewController alloc] init];
    [self.navigationController pushViewController:moreVC animated:YES];
}

#pragma mark - 创建CollectionView
/**
 *  每日精选
 */
- (void)creatCollectionView
{
    //初始化UICollectionViewFlowLayout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //设置上下间距为0
    layout.minimumInteritemSpacing = 0;
    //设置左右间距为0
    layout.minimumLineSpacing = 0;
    //设置footer的大小
    layout.footerReferenceSize = CGSizeMake(kWidth, 40);
    //设置UICollectionView的大小,并初始化
    _collection = [[DailyCollectionViewController alloc] initWithCollectionViewLayout:layout];
 
    
    self.tag = 200;
    //跳转到详情页
    __weak DailyViewController *dailyVC = self;
    _collection.pushBlock=^(NSArray *array,NSInteger index,NSInteger day)
    {
        TotalScrollViewController *totalSVC = [[TotalScrollViewController alloc] init];
        //加载出来的数据
        totalSVC.dataSource = [array mutableCopy];
        //点击的那一行的下标
        totalSVC.idx = index;
        //点击的那一天的下标
        totalSVC.day = day;
        //跳转
        [dailyVC.navigationController pushViewController:totalSVC animated:YES];
    };
    
    //添加到视图上
    [self.view addSubview:_collection.view];
    
    //设置大小
    _collection.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    //设置UICollectionView的背景颜色
    _collection.collectionView.backgroundColor = [UIColor whiteColor];
}
/**
 *  添加往期分类
 */
- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //  设置上下之间的距离和左右之间的距离
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    //  设置item的大小
    layout.itemSize = CGSizeMake((SCREEN_WIDTH - 5) / 2, (SCREEN_WIDTH - 5) / 2);
    _kindController = [[KindsCollectionViewController alloc] initWithCollectionViewLayout:layout];
    self.tag = 100;
    
    
    //跳转到详情页
    __weak DailyViewController *dailyVC = self;
    _kindController.kindsBlock=^(CategoryVideo *model)
    {
        UICollectionViewFlowLayout *kLayout = [[UICollectionViewFlowLayout alloc] init];
        
        kLayout.minimumInteritemSpacing = 0;
        kLayout.minimumLineSpacing = 0;
        //  设置item的大小
        kLayout.itemSize = CGSizeMake((SCREEN_WIDTH - 5) / 2, (SCREEN_WIDTH - 5) / 2);
        
        KindsInfoController *totalSVC = [[KindsInfoController alloc] initWithCollectionViewLayout:kLayout];
        //加载出来的数据
        totalSVC.model = model;
        //跳转
        [dailyVC.navigationController pushViewController:totalSVC animated:YES];
    };
    
    
    [self.view addSubview:_kindController.view];
    _kindController.view.frame = self.view.bounds;
    _kindController.collectionView.backgroundColor = [UIColor whiteColor];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
